<?php

namespace App\Admin\Controllers;

use App\Models\Stock;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class StockController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '产品管理';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Stock);

        $grid->column('id', __('Id'));
        $grid->column('code', __('产品code'));
        $grid->column('name', __('产品名称'));
        $grid->column('price', __('当前价格'));

        $grid->column('rebate', __('返利'));
        $grid->column('rebate_cycle', __('返利周期(单位小时)'));
        $grid->column('rebate_type', __('返利类型'))->display(function ($status) {
            return config('conf.type')[$status];
        });
        $grid->column('on_sale', __('状态'))->display(function ($status) {
            return config('conf.status')[$status];
        });
        $grid->column('created_at', __('创建时间'));
        $grid->column('updated_at', __('修改时间'));
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Stock::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('code', __('产品code'));
        $show->field('name', __('产品名称'));
        $show->field('price', __('当前价格'));
        $show->field('rebate', __('返利'));
        $show->field('rebate_cycle', __('返利周期(单位小时)'));
        $show->field('rebate_type', __('返利类型'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));
        $show->field('on_sale', __('状态'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Stock);
        $form->text('code', __('产品code'));
        $form->text('name', __('产品名称'));

        $form->decimal('price', __('当前价格'));
        $form->number('rebate', __('返利'));
        $form->number('rebate_cycle', __('返利周期(单位小时)'));
        $form->select('rebate_type', __('返利类型'))->options(config('conf.type'));
        $form->select('on_sale', __('状态'))->options(config('conf.status'));

//

        return $form;
    }
}
